using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
//using Microsoft.Office.Server.ApplicationRegistry.MetadataModel;
//using Microsoft.Office.Server.ApplicationRegistry.Runtime;

namespace PS.WebParts.Rollup
{
    public class BusinessDataView : ATabularDataSourceView
    {
        private string lobInstanceName = "";
        protected DataTable bdcData;
        protected DataSet dsData;
        private string entityName = "";
        private List<BusinessDataFilter> entityFilters;
        private string fieldNames;

        public string FieldNames
        {
            get { return fieldNames; }
            set { fieldNames = value; }
        }

        public string LobInstanceName
        {
            get { return lobInstanceName; }
            set { lobInstanceName = value; }
        }

        public string EntityName
        {
            get { return entityName; }
            set { entityName = value; }
        }

        public List<BusinessDataFilter> EntityFilters
        {
            get
            {
                if (entityFilters == null)
                    entityFilters = new List<BusinessDataFilter>();
                return entityFilters;
            }
            set
            {
                entityFilters = value;
            }
        }


        public BusinessDataView(HttpContext context, IDataSource owner, string viewName)
            : base(context, owner, viewName)
        {
        }

        public override DataTable GetData()
        {
            QueryBdc();
            return bdcData;
            //base.SetReturnType(bdcData);
            //return bdcData != null ? new DataView(bdcData) : null;
        }

        public IEnumerable Test()
        {
            return ExecuteSelect(null);
        }

        public override DataTable GetSchema()
        {
            return SetUpDataTable(FieldNames);
        }

        protected DataTable SetUpDataTable(string columns)
        {
            DataTable table = new DataTable("Row");

            if (!string.IsNullOrEmpty(FieldNames))
            {
                foreach (string columnName in columns.Split(','))
                {
                    table.Columns.Add(new DataColumn(columnName, typeof(string)));
                }
            }

            return table;
        }
        protected DataSet GetDataSet()
        {
            DataSet ds = new DataSet("Rows");
            bdcData = GetSchema();
            ds.Tables.Add(bdcData);
            return ds;
        }
        protected void QueryBdc()
        {
            //IEntityInstanceEnumerator entityEnum = null;
            //NamedLobSystemInstanceDictionary bdcInstances = ApplicationRegistry.GetLobSystemInstances();
            //if (bdcInstances.ContainsKey(LobInstanceName))
            //{
            //    LobSystemInstance myInstance = bdcInstances[LobInstanceName];
            //    NamedEntityDictionary bdcEntities = myInstance.GetEntities();
            //    if (bdcEntities.ContainsKey(EntityName))
            //    {
            //        Entity myEntity = bdcEntities[EntityName];
            //        FilterCollection fc = myEntity.GetFinderFilters();
            //        foreach (BusinessDataFilter f in EntityFilters)
            //        {
            //            for (int i = 0; i < fc.Count; i++)
            //            {
            //                if (fc[i].Name == f.FilterName)
            //                {
            //                    string val = ARollupDataSourceView.ReplaceQueryLiveFields(f.FilterValue, SPContext.Current.Web, Context, null, null);
            //                    switch (fc[i].GetType().FullName)
            //                    {                                    
            //                        case "Microsoft.Office.Server.ApplicationRegistry.Runtime.WildcardFilter":
            //                            ((WildcardFilter)fc[i]).Value = val;
            //                            break;

            //                        case "Microsoft.Office.Server.ApplicationRegistry.Runtime.ComparisonFilter":
            //                            ((ComparisonFilter)fc[i]).Value = val;
            //                            break;
            //                        case "Microsoft.Office.Server.ApplicationRegistry.Runtime.LimitFilter":
            //                            ((LimitFilter)fc[i]).Value = uint.Parse(val);
            //                            break;
            //                    }
            //                }
            //            }

            //        }
            //        try
            //        {
            //            entityEnum = myEntity.FindFiltered(fc, myInstance);
            //        }
            //        catch (ObjectNotFoundException)
            //        { }
            //        dsData = GetDataSet();
            //        if (entityEnum != null)
            //        {
            //            while (entityEnum.MoveNext())
            //            {
            //                IEntityInstance ie = entityEnum.Current;
            //                DataRow r = bdcData.NewRow();
            //                foreach (string col in FieldNames.Split(','))
            //                {
            //                    string c = col.Trim();
            //                    if (ie[c] != null)
            //                        r[c] = ie[c];
            //                }
            //                bdcData.Rows.Add(r);
            //            }
            //            entityEnum.Close();
            //        }
            //    }
            //}
        }


    }
}
